/*
 * 文 件 名 : ContactsActivity.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : wangchd
 * 修改时间 : 2013-2-18
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.ui.contacts;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.fire8.dolphin.ui.R;
import com.fire8.dolphin.utils.LogUtil;
import com.fire8.dolphin.utils.StringUtil;

/**
 * 弹出联系人列表
 * <功能详细描述>
 *
 * @author  wangchd
 * @version [版本号,2013-2-18]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class ContactsActivity extends Activity
{
    
    EditText edInput;
    
    ListView lvContacts;
    
    Button btSure;
    
    private ArrayList<HashMap<String,String>> selectNames = new ArrayList<HashMap<String,String>>();
    private ArrayList<HashMap<String,String>> nameMap = new ArrayList<HashMap<String,String>>();
    private MyAdapter lvMyAdapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts);
        edInput = (EditText)findViewById(R.id.ed_input);
        btSure = (Button)findViewById(R.id.bt_sure);
        lvContacts = (ListView)findViewById(R.id.lv_contacts);
        lvMyAdapter = new MyAdapter(ContactsActivity.this);
        lvContacts.setAdapter(lvMyAdapter);
        new GetContactsTask().execute("");
        btSure.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                Intent intent= new Intent();
                if(!StringUtil.isEmpty(edInput.getText().toString())){
                    HashMap<String,String> tempMap = new HashMap<String,String>();
                    tempMap.put("phone",edInput.getText().toString());
                    tempMap.put("name",edInput.getText().toString());
                    selectNames.add(tempMap);
                }
                if(!selectNames.isEmpty()){
                    intent.putExtra("selectNames", selectNames);
                }
                setResult(RESULT_OK, intent);
                ContactsActivity.this.finish();
            }
        });
    }
    
    public class MyAdapter extends BaseAdapter
    {
        private LayoutInflater mInflater;
        private HashMap<Integer,View> viewMap;
        public MyAdapter(Context context)
        {
            mInflater = LayoutInflater.from(context);
            viewMap = new HashMap<Integer, View>();
        }
        
        @Override
        public int getCount()
        {
            return nameMap.size();
        }
        
        @Override
        public Object getItem(int position)
        {
            return nameMap.get(position);
        }
        
        @Override
        public long getItemId(int position)
        {
            return 0;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View rowView = viewMap.get(position);
            ViewHolder holder = null;
            if (rowView == null)
            {
                holder = new ViewHolder();
                rowView = mInflater.inflate(R.layout.contacts_items, null);
                holder.tvName = (TextView)rowView.findViewById(R.id.tv_name);
                holder.tvPhone = (TextView)rowView.findViewById(R.id.tv_phone);
                holder.cbSelect = (CheckBox)rowView.findViewById(R.id.cb_select);
                rowView.setTag(holder);
                viewMap.put(position, rowView);
            }
            else
            {
                holder = (ViewHolder)rowView.getTag();
            }
            HashMap<String,String> tempMap = nameMap.get(position);
            final String  sname = tempMap.get("name");
            final String sphone = tempMap.get("phone");
            holder.tvName.setText(sname);
            holder.tvPhone.setText(sphone);
            holder.cbSelect.setOnCheckedChangeListener(new OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    HashMap<String,String> tempMap = new HashMap<String,String>();
                    if(isChecked){
                        tempMap.put("name", sname);
                        tempMap.put("phone", sphone);
                        selectNames.add(tempMap);
                    }else{
                        selectNames.remove(tempMap);
                    }
                }
            });
            return rowView;
        }
        public final class ViewHolder
        {
            public TextView tvName;
            
            public TextView tvPhone;
            
            public CheckBox cbSelect;
            
        }
    }
    /**异步获取通讯录**/
    private class GetContactsTask extends AsyncTask<String, Void, String> 
    {
        @Override
        protected String doInBackground(String... arg0)
        {
            //Long start = System.currentTimeMillis();
            Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
            try
            {
                while (cursor.moveToNext())
                {
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace("-", "").replace(" ", "");
                    HashMap<String,String> phoneItems = new HashMap<String,String>();
                    phoneItems.put("name", name);
                    phoneItems.put("phone",phoneNumber);
                    nameMap.add(phoneItems);
                    
                }
            }
            catch (Exception e)
            {
                LogUtil.e("", e);
            }finally
            {
                if(cursor != null)
                {
                    cursor.close();
                }
            }
            
            //Long end = System.currentTimeMillis();
            //LogUtil.d("花费时间："+(end-start));
            return null;
        }

        @Override
        protected void onPostExecute(String result)
        {
            super.onPostExecute(result);
            lvMyAdapter.notifyDataSetChanged();
        }
        
        
    }
}
